﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PMisuseAn.io;
using System.Collections.Specialized;
using System.Collections;
using PMisuseAn.util;

namespace PMisuseAn
{
    /// <summary>
    /// Interaction logic for AddAggregateFromToAttributeWindow1.xaml
    /// </summary>
    public partial class AddAggregateFromToAttributeWindow1 : Window
    {
        //public static string formula_ = "";
        
        AddActorAttributeWindow aaaw_;
        AddImpliedFromToAttributeWindow aiftaw_;
        
        List<Attribute> agentAttributes;
        List<Attribute> functionalAttributes;
        List<Attribute> environmentalAttributes;
        List<Attribute> regulatoryAttributes;

        List<string> groupNames;
        List<string> actorNames;

        private ListDictionary if_rc_formula = new ListDictionary();
        private ListDictionary if_then_rc_formula = new ListDictionary();
        private ListDictionary if_then_unless_rc_formula = new ListDictionary();

        private ListDictionary do_rc_formula = new ListDictionary();
        private ListDictionary do_until_rc_formula = new ListDictionary();
        private ListDictionary do_until_unless_rc_formula = new ListDictionary();

        private List<string> attList = new List<string>();
        private int comp_id = 1;
        
        List<string> agents;

        Attribute selectedAttribute;
        private Attribute source_attribute_;

        //bool if_rule_active = false;

        bool functionalSpecComplete = true;
        bool groupfunctionalSpecComplete = true;

        List<string> functionalParameters = new List<string>();
        List<string> groupFunctionalParameters = new List<string>();

        bool firstThenAttExe = false;
        //bool firstThenUnlessAttExe = false;
        bool aggIfThenFirstCall = false;

        public AddAggregateFromToAttributeWindow1()
        {
            InitializeComponent();
            this.Left = 20;
            this.Top = 25;

            agentAttributes = new List<Attribute>();
            functionalAttributes = new List<Attribute>();
            environmentalAttributes = new List<Attribute>();
            regulatoryAttributes = new List<Attribute>();
            actorNames = new List<string>();
            groupNames = new List<string>();

            loadAttributesToCategoryList();
            reloadAggregationStatementsTable();

            agents = MisUseAnObjectsDB.loadAllAgentNames(MainWindow.clusterName);

            //reinitialiseActiveInactiveComps();

        }

        public AddAggregateFromToAttributeWindow1(AddActorAttributeWindow aaaw, Attribute source_attribute)
        {
            InitializeComponent();
            source_attribute_ = source_attribute;
            this.Title = this.Title + ":" + source_attribute.name;
            aaaw_ = aaaw;
            this.Left = 20;
            this.Top = 25;

            agentAttributes = new List<Attribute>();
            functionalAttributes = new List<Attribute>();
            environmentalAttributes = new List<Attribute>();
            regulatoryAttributes = new List<Attribute>();
            actorNames = new List<string>();
            groupNames = new List<string>();

            loadAttributesToCategoryList();
            reloadAggregationStatementsTable();
            
            agents = MisUseAnObjectsDB.loadAllAgentNames(MainWindow.clusterName);

            //reinitialiseActiveInactiveComps();
        }


        public AddAggregateFromToAttributeWindow1(AddImpliedFromToAttributeWindow aiftaw, Attribute source_attribute)
        {
            InitializeComponent();
            aiftaw_ = aiftaw;
            source_attribute_ = source_attribute;
            this.Title = this.Title + ":" + source_attribute.name;
            this.Left = 20;
            this.Top = 25;

            agentAttributes = new List<Attribute>();
            functionalAttributes = new List<Attribute>();
            environmentalAttributes = new List<Attribute>();
            regulatoryAttributes = new List<Attribute>();
            actorNames = new List<string>();
            groupNames = new List<string>();

            loadAttributesToCategoryList();
            reloadAggregationStatementsTable();

            agents = MisUseAnObjectsDB.loadAllAgentNames(MainWindow.clusterName);

            //reinitialiseActiveInactiveComps();
        }

        List<string> noOpenSym = new List<string>();

        private void autoCloseSymbols()
        {
            foreach (var x in noOpenSym)
            {
                if_then_rc_tb.AppendText(") ");
                if_then_rc_formula.Add("symbol_" + comp_id, ") ");
                comp_id = comp_id + 1;                
            }
            noOpenSym = new List<string>();

            //or.IsEnabled = true;
            //and.IsEnabled = true;
        }

        private void Symbol_Auto_Close_Button_Click(object sender, RoutedEventArgs e)
        {
            autoCloseSymbols();
        }        

        private void Symbol_Button_Click(object sender, RoutedEventArgs e)
        {
            Button symbol_b = (Button)sender;
            string symbol = (string)symbol_b.Content;

            
            if (symbol != null)
            {
                if (symbol.Equals("¬") || symbol.Equals("∧") || symbol.Equals("V") || symbol.Equals("⊨"))
                {
                    if (noOpenSym.Count > 0)
                    {
                        
                        if_then_rc_tb.AppendText(") ");
                        if_then_rc_formula.Add("symbol_" + comp_id, ") ");
                        comp_id = comp_id + 1;

                        if_then_rc_tb.AppendText(symbol + " ");
                        if_then_rc_formula.Add("symbol_" + comp_id, symbol);
                        comp_id = comp_id + 1;

                        if_then_rc_tb.AppendText("( ");
                        if_then_rc_formula.Add("symbol_" + comp_id, "( ");
                        comp_id = comp_id + 1;
                        
                    }
                    else
                    {
                        if_then_rc_tb.AppendText(symbol + " ");
                        if_then_rc_formula.Add("symbol_" + comp_id, symbol);
                        comp_id = comp_id + 1;

                        if_then_rc_tb.AppendText("( ");
                        if_then_rc_formula.Add("symbol_" + comp_id, "( ");
                        comp_id = comp_id + 1;                       

                        noOpenSym.Add("(");
                    }
                }
                else
                {
                    if_then_rc_tb.AppendText(symbol + " ");
                    if_then_rc_formula.Add("symbol_" + comp_id, symbol);
                    comp_id = comp_id + 1;
                    
                }
                
                //activeInactiveCompsSymbolSelection(symbol);
            }
        }

        private void Clear_Button_Click(object sender, RoutedEventArgs e)
        {
            clearAll();
        }

        private void clearAll()
        {
            clearFormulaBox();
            
            firstThenAttExe = false;
            aggIfThenFirstCall = false;

            functionalSpecComplete = true;
            functionalParameters = new List<string>();
            noOpenSym = new List<string>();
            attList = new List<string>();
            
            do_rc_formula.Clear();
            do_until_rc_formula.Clear();
            do_until_unless_rc_formula.Clear();
            if_rc_formula.Clear();
            if_then_rc_formula.Clear();
            if_then_unless_rc_formula.Clear();

            domain_l_t.Content = "";
            comp_id = 1;
        }

        private void clearFormulaBox()
        {
            if_then_rc_tb.Document = new FlowDocument();
        }
        
        public void loadAttributesToCategoryList()
        {
            List<string> agentAttributes_s = new List<string>();
            List<string> functionalAttributes_s = new List<string>();
            List<string> environmentalAttributes_s = new List<string>();
            List<string> regulatoryAttributes_s = new List<string>();

            agentAttributes = MisUseAnObjectsDB.loadAgentAttributes(MainWindow.clusterName);
            functionalAttributes = MisUseAnObjectsDB.loadFunctionalAttributes(MainWindow.clusterName);
            environmentalAttributes = MisUseAnObjectsDB.loadEnvironmentAttributes(MainWindow.clusterName);
            regulatoryAttributes = MisUseAnObjectsDB.loadRegulatoryAttributes(MainWindow.clusterName);

            foreach (Attribute att in agentAttributes)
            {
                agentAttributes_s.Add(att.name);
            }
            agentAttributes_lb.ItemsSource = agentAttributes_s;
            
            foreach (Attribute att in environmentalAttributes)
            {
                environmentalAttributes_s.Add(att.name);
            }
            environmentAttributes_lb.ItemsSource = environmentalAttributes_s;

            foreach (Attribute att in regulatoryAttributes)
            {
                regulatoryAttributes_s.Add(att.name);
            }
            regulatoryAttributes_lb.ItemsSource = regulatoryAttributes_s;
        }

        //public void reinitialiseActiveInactiveComps()
        //{
        //    if (functionalSpecComplete == false || groupfunctionalSpecComplete == false)
        //    {
        //        return;
        //    }

        //    TextRange textRange = new TextRange(if_then_rc_tb.Document.ContentStart,
        //        if_then_rc_tb.Document.ContentEnd);
        //    string formula_statement_sub = textRange.Text;

        //    if(formula_statement_sub != null && formula_statement_sub.Count()>0)
        //    {
        //        union.IsEnabled = false;
        //        intersection.IsEnabled = false;
        //        or.IsEnabled = true;
        //        and.IsEnabled = true;

        //        agentAttributes_lb.IsEnabled = true;
        //        environmentAttributes_lb.IsEnabled = true;
        //    }
        //    else
        //    {
        //        union.IsEnabled = false;
        //        intersection.IsEnabled = false;
        //        or.IsEnabled = false;
        //        and.IsEnabled = false;

        //        agentAttributes_lb.IsEnabled = false;
        //        environmentAttributes_lb.IsEnabled = false;
        //    }
        //}

        //public void activeInactiveCompsSymbolSelection(String symbol)
        //{
        //    if (functionalSpecComplete == false || groupfunctionalSpecComplete == false)
        //    {
        //        return;
        //    }

        //     TextRange textRange = new TextRange(if_then_rc_tb.Document.ContentStart,
        //        if_then_rc_tb.Document.ContentEnd);
        //    string formula_statement_sub = textRange.Text;

        //    if(formula_statement_sub != null && formula_statement_sub.Count()>0)
        //    {
        //        agentAttributes_lb.IsEnabled = true;
        //        environmentAttributes_lb.IsEnabled = true;

        //        union.IsEnabled = false;
        //        intersection.IsEnabled = false;
        //        or.IsEnabled = true;
        //        and.IsEnabled = true;                
        //    }
        //    else
        //    {
        //        agentAttributes_lb.IsEnabled = false;
        //        environmentAttributes_lb.IsEnabled = false;

        //        union.IsEnabled = false;
        //        intersection.IsEnabled = false;
        //        or.IsEnabled = false;
        //        and.IsEnabled = false;
        //    }

        //}

        //public void activeInactiveCompsSymbolSelection()
        //{
        //    if (functionalSpecComplete == false || groupfunctionalSpecComplete == false)
        //    {
        //        return;
        //    }
        //      TextRange textRange = new TextRange(if_then_rc_tb.Document.ContentStart,
        //        if_then_rc_tb.Document.ContentEnd);
        //    string formula_statement_sub = textRange.Text;

        //    if(formula_statement_sub != null && formula_statement_sub.Count()>0)
        //    {
        //        agentAttributes_lb.IsEnabled = true;
        //        environmentAttributes_lb.IsEnabled = true;

        //        union.IsEnabled = false;
        //        intersection.IsEnabled = false;
        //        or.IsEnabled = true;

        //        or.IsEnabled = true;
        //        and.IsEnabled = true;
        //    }
        //    else
        //    {
        //        agentAttributes_lb.IsEnabled = true;
        //        environmentAttributes_lb.IsEnabled = true;

        //        union.IsEnabled = false;
        //        intersection.IsEnabled = false;

        //        or.IsEnabled = false;
        //        and.IsEnabled = false;
        //    }

        //}
        
        private void add_policy_statement_b_Click(object sender, RoutedEventArgs e)
        {   
            string formula_statement = "";

            if (if_then_rc_tb.Document != null)
            {
                TextRange textRange = new TextRange(if_then_rc_tb.Document.ContentStart,
                if_then_rc_tb.Document.ContentEnd);
                string formula_statement_sub = textRange.Text;
                formula_statement_sub = formula_statement_sub.TrimEnd('\n');
                formula_statement_sub = formula_statement_sub.TrimEnd('\r');

                
                if (formula_statement_sub.Length > 0)
                {
                    formula_statement = formula_statement + formula_statement_sub;
                }
            }

           
            if (formula_statement.Length > 0)
            {
                MisUseAnObjectsDB.addAggregationStatement(formula_statement, MainWindow.clusterName);
                reloadAggregationStatementsTable();
                clearFormulaBox();
                clearAll();

                //activeInactiveCompsSymbolSelection();
            }
        }
        
        private void reloadAggregationStatementsTable()
        {
            List<string> agg_statements = MisUseAnObjectsDB.loadAllAggregationStatements(MainWindow.clusterName);

            if (agg_statements != null)
            {
                policies_lb.Items.Clear();
                int pol_no = 1;

                foreach (string agg_stat in agg_statements)
                {
                    ListBoxItem l = new ListBoxItem();
                    l.FontSize = 10.5;
                    StackPanel s = new StackPanel();
                    s.Orientation = System.Windows.Controls.Orientation.Horizontal;

                    ContentPresenter c1 = new ContentPresenter();
                    c1.Content = pol_no + ".";
                    pol_no = pol_no + 1;

                    ContentPresenter c3 = new ContentPresenter();

                    if (policies_lb.Items.Count % 2 == 0)
                    {
                        l.Background = Brushes.AliceBlue;
                    }
                    c3.Content = agg_stat;

                    c1.Width = 15;
                    //c2.Width = 98;
                    c3.Width = policies_lb.Width - 25;
                    s.Children.Add(c1);
                    //s.Children.Add(c2);
                    s.Children.Add(c3);

                    //add context menu to item to clone state machine
                    //use cloneStateMachineDB method in DB class

                    ContextMenu cm = new System.Windows.Controls.ContextMenu();
                    cm.Height = 64;
                    cm.Width = 136;

                    MenuItem cm_d = new MenuItem();
                    cm_d.Header = "Delete";
                    cm_d.Tag = agg_stat;
                    cm_d.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(del_aggregation_statement_b_Click));
                    cm_d.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    cm_d.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                    cm_d.FontSize = 12;
                    cm_d.Foreground = Brushes.DarkGreen;

                    BitmapImage cm_d_image = new BitmapImage();
                    cm_d_image.BeginInit();
                    Uri cm_d_Uri = new Uri("/PMisuseAn;component/images/DeleteHS.png", UriKind.RelativeOrAbsolute);
                    cm_d_image.UriSource = cm_d_Uri;
                    cm_d_image.EndInit();

                    Image cm_d_iconImage = new Image();
                    cm_d_iconImage.Source = cm_d_image;
                    cm_d.Icon = cm_d_iconImage;

                    cm.Items.Add(cm_d);
                    l.ContextMenu = cm;

                    l.Content = s;
                    l.Tag = agg_stat;

                    policies_lb.Items.Add(l);
                }
            }
        }

        void del_aggregation_statement_b_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Confirm delete...", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                return;
            }
            MenuItem d_b = (MenuItem)sender;
            string tag_id = (string)d_b.Tag;
            MisUseAnObjectsDB.deleteAggregationStatement(tag_id, MainWindow.clusterName);

            reloadAggregationStatementsTable();
        } 
                
        public bool missingBracket_if_rc_formula(string formula_statement_sub)
        {
            int open_bracket = 0;
            int close_bracket = 0;
            foreach (DictionaryEntry f_s in if_rc_formula)
            {
                if (f_s.Value != null && f_s.Value.Equals("("))
                {
                    open_bracket = open_bracket + 1;
                }
                else if (f_s.Value != null && f_s.Value.Equals(")"))
                {
                    close_bracket = close_bracket + 1;
                }
            }
            if (close_bracket == open_bracket)
            {
                return false;
            }
            return true;
        }

        public bool missingBracket_if_then_rc_formula(string formula_statement_sub)
        {
            int open_bracket = 0;
            int close_bracket = 0;
            foreach (DictionaryEntry f_s in if_then_rc_formula)
            {
                if (f_s.Value.Equals("("))
                {
                    open_bracket = open_bracket + 1;
                }
                else if (f_s.Value.Equals(")"))
                {
                    close_bracket = close_bracket + 1;
                }
            }
            if (close_bracket == open_bracket)
            {
                return false;
            }
            return true;
        }

        public bool missingBracket_if_then_unless_rc_formula(string formula_statement_sub)
        {
            int open_bracket = 0;
            int close_bracket = 0;
            foreach (DictionaryEntry f_s in if_then_unless_rc_formula)
            {
                if (f_s.Value.Equals("("))
                {
                    open_bracket = open_bracket + 1;
                }
                else if (f_s.Value.Equals(")"))
                {
                    close_bracket = close_bracket + 1;
                }
            }
            if (close_bracket == open_bracket)
            {
                return false;
            }
            return true;
        }

        public bool missingBracket_do_rc_formula(string formula_statement_sub)
        {
            int open_bracket = 0;
            int close_bracket = 0;
            foreach (DictionaryEntry f_s in do_rc_formula)
            {
                if (f_s.Value.Equals("("))
                {
                    open_bracket = open_bracket + 1;
                }
                else if (f_s.Value.Equals(")"))
                {
                    close_bracket = close_bracket + 1;
                }
            }
            if (close_bracket == open_bracket)
            {
                return false;
            }
            return true;
        }

        public bool missingBracket_do_until_rc_formula(string formula_statement_sub)
        {
            int open_bracket = 0;
            int close_bracket = 0;
            foreach (DictionaryEntry f_s in do_until_rc_formula)
            {
                if (f_s.Value.Equals("("))
                {
                    open_bracket = open_bracket + 1;
                }
                else if (f_s.Value.Equals(")"))
                {
                    close_bracket = close_bracket + 1;
                }
            }
            if (close_bracket == open_bracket)
            {
                return false;
            }
            return true;
        }

        public bool missingBracket_do_until_unless_rc_formula(string formula_statement_sub)
        {
            int open_bracket = 0;
            int close_bracket = 0;
            foreach (DictionaryEntry f_s in do_until_unless_rc_formula)
            {
                if (f_s.Value.Equals("("))
                {
                    open_bracket = open_bracket + 1;
                }
                else if (f_s.Value.Equals(")"))
                {
                    close_bracket = close_bracket + 1;
                }
            }
            if (close_bracket == open_bracket)
            {
                return false;
            }
            return true;
        }


        private void regulatoryAttributes_lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (regulatoryAttributes_lb.SelectedItem == null)
            {
                return;
            }
            string selected_regulatory_att = regulatoryAttributes_lb.SelectedItem.ToString();
            if (functionalSpecComplete == true && groupfunctionalSpecComplete == true)
            {
                attList.Add(selected_regulatory_att);
                if (firstThenAttExe == true && attList.Count > 2)
                {
                    if_then_rc_tb.AppendText("∩ ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "∩ ");
                    comp_id = comp_id + 1;
                }

                if_then_rc_tb.AppendText(selected_regulatory_att + " ");
                if_then_rc_formula.Add("attribute_" + comp_id, selected_regulatory_att);
                comp_id = comp_id + 1;

                if (firstThenAttExe == false)
                {
                    if_then_rc_tb.AppendText("⊨ ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "⊨ ");
                    comp_id = comp_id + 1;

                    if_then_rc_tb.AppendText("( ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "( ");
                    comp_id = comp_id + 1;

                    noOpenSym.Add("(");
                    firstThenAttExe = true;
                }

                string attribute_domain = "";
                List<string> att_dom_names = new List<string>();
                foreach (Attribute att in regulatoryAttributes)
                {
                    if (att.name.Equals(selected_regulatory_att))
                    {
                        //int i = 0;
                        foreach (string s in att.domain)
                        {
                            att_dom_names.Add(s);
                        }
                        break;
                    }
                }
                if (selectedAttribute != null)
                {
                    domain_l_t.Content = attribute_domain + " " + selectedAttribute.literalType;
                }

                if (aggIfThenFirstCall == false)
                {
                    aggIfThenFirstCall = true;
                }
                else
                {
                    //activeInactiveCompsLBSelection();
                }
            }
            else if (functionalSpecComplete == true && groupfunctionalSpecComplete == false)
            {
                MessageBox.Show(this, "group expected.",
                                       "Policy definition error", MessageBoxButton.OK,
                                       MessageBoxImage.Warning, MessageBoxResult.Cancel);
                return;

            }
            else if (functionalSpecComplete == false && groupfunctionalSpecComplete == true)
            {
                if (!(functionalParameters.Count() == 1))
                {
                    MessageBox.Show(this, "actor role/group expected.",
                                       "Policy definition error", MessageBoxButton.OK,
                                       MessageBoxImage.Warning, MessageBoxResult.Cancel);
                    return;
                }

                functionalParameters.Add(selected_regulatory_att);
                if (functionalParameters.Count() == 3)
                {
                    //functionalAttributes_lb.IsEnabled = true;
                    functionalSpecComplete = true;
                    selected_regulatory_att = selected_regulatory_att + " )";
                    //activeInactiveCompsLBSelection();
                    functionalParameters = new List<string>();
                }
                else
                {
                    selected_regulatory_att = selected_regulatory_att + " , ";
                }
                if_then_rc_tb.AppendText(selected_regulatory_att + " ");
                if_then_rc_formula.Add("attribute_" + comp_id, selected_regulatory_att);
                comp_id = comp_id + 1;

            }

            regulatoryAttributes_lb.SelectedIndex = -1;
        }
        

        private void agentAttributes_lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (agentAttributes_lb.SelectedItem == null)
            {
                return;
            }
            string selected_agent_att = agentAttributes_lb.SelectedItem.ToString();
            if (functionalSpecComplete == true && groupfunctionalSpecComplete ==true)
            {
                attList.Add(selected_agent_att);
                if (firstThenAttExe == true && attList.Count > 2)
                {
                    if_then_rc_tb.AppendText("∩ ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "∩ ");
                    comp_id = comp_id + 1;
                }

                if_then_rc_tb.AppendText(selected_agent_att + " ");
                if_then_rc_formula.Add("attribute_" + comp_id, selected_agent_att);
                comp_id = comp_id + 1;

                if (firstThenAttExe == false)
                {
                    if_then_rc_tb.AppendText("⊨ ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "⊨ ");
                    comp_id = comp_id + 1;

                    if_then_rc_tb.AppendText("( ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "( ");
                    comp_id = comp_id + 1;

                    noOpenSym.Add("(");
                    firstThenAttExe = true;
                }

                string attribute_domain = "";
                List<string> att_dom_names = new List<string>();
                foreach (Attribute att in agentAttributes)
                {
                    if (att.name.Equals(selected_agent_att))
                    {
                        //int i = 0;
                        foreach (string s in att.domain)
                        {
                            att_dom_names.Add(s);
                        }
                        break;
                    }
                }
                if (selectedAttribute != null)
                {
                    domain_l_t.Content = attribute_domain + " " + selectedAttribute.literalType;
                }

                if (aggIfThenFirstCall == false)
                {
                    aggIfThenFirstCall = true;
                }
                else
                {
                    //activeInactiveCompsLBSelection();
                }
            }
            else if (functionalSpecComplete == true && groupfunctionalSpecComplete == false)
            {
                MessageBox.Show(this, "group expected.",
                                       "Policy definition error", MessageBoxButton.OK,
                                       MessageBoxImage.Warning, MessageBoxResult.Cancel);
                return;

            }
            else if (functionalSpecComplete == false && groupfunctionalSpecComplete == true)
            {
                if (!(functionalParameters.Count() == 1))
                {
                    MessageBox.Show(this, "actor role/group expected.",
                                       "Policy definition error", MessageBoxButton.OK,
                                       MessageBoxImage.Warning, MessageBoxResult.Cancel);
                    return;
                }

                functionalParameters.Add(selected_agent_att);
                if (functionalParameters.Count() == 3)
                {
                    //functionalAttributes_lb.IsEnabled = true;
                    functionalSpecComplete = true;
                    selected_agent_att = selected_agent_att + " )";
                    //activeInactiveCompsLBSelection();
                    functionalParameters = new List<string>();
                }
                else
                {
                    selected_agent_att = selected_agent_att + " , ";
                }
                if_then_rc_tb.AppendText(selected_agent_att + " ");
                if_then_rc_formula.Add("attribute_" + comp_id, selected_agent_att);
                comp_id = comp_id + 1;               

            }
            
            agentAttributes_lb.SelectedIndex = -1;
        }
        
        private void environmentalAttributes_lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (environmentAttributes_lb.SelectedItem == null)
            {
                return;
            }

            string selected_env_att = environmentAttributes_lb.SelectedItem.ToString();
            if (functionalSpecComplete == true && groupfunctionalSpecComplete == true)
            {
                attList.Add(selected_env_att);
                if (firstThenAttExe == true && attList.Count > 2)
                {
                    if_then_rc_tb.AppendText("∩ ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "∩ ");
                    comp_id = comp_id + 1;
                }

                if_then_rc_tb.AppendText(selected_env_att + " ");
                if_then_rc_formula.Add("attribute_" + comp_id, selected_env_att);
                comp_id = comp_id + 1;
                    
                if (firstThenAttExe == false)
                {
                    if_then_rc_tb.AppendText("⊨ ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "⊨ ");
                    comp_id = comp_id + 1;

                    if_then_rc_tb.AppendText("( ");
                    if_then_rc_formula.Add("symbol_" + comp_id, "( ");
                    comp_id = comp_id + 1;

                    noOpenSym.Add("(");
                    firstThenAttExe = true;
                }

                string attribute_domain_long = "";
                string attribute_domain = "";
                List<string> att_dom_names = new List<string>();
                foreach (Attribute att in environmentalAttributes)
                {
                    if (att.name.Equals(selected_env_att))
                    {
                        selectedAttribute = att;
                       
                        //int i = 0;
                        foreach (string s in att.domain)
                        {
                            att_dom_names.Add(s);
                        }
                        break;
                    }
                }
                domain_l_t.Content = attribute_domain + " " + selectedAttribute.literalType;
                domain_l_t.ToolTip = attribute_domain_long;
                //reloadDomainInComboBox(att_dom_names);

                if (aggIfThenFirstCall == false)
                {
                    aggIfThenFirstCall = true;
                }
                else
                {
                    //activeInactiveCompsLBSelection();
                }
            }
            else if (functionalSpecComplete == true && groupfunctionalSpecComplete == false)
            {
                MessageBox.Show(this, "group expected.",
                                        "Policy definition error", MessageBoxButton.OK,
                                        MessageBoxImage.Warning, MessageBoxResult.Cancel);
                return;
                
            }
            else if (functionalSpecComplete == false && groupfunctionalSpecComplete == true)
            {
                if (!(functionalParameters.Count() == 1))
                {
                    MessageBox.Show(this, "actor role/group expected.",
                                       "Policy definition error", MessageBoxButton.OK,
                                       MessageBoxImage.Warning, MessageBoxResult.Cancel);
                    return;
                }
                functionalParameters.Add(selected_env_att);
                if (functionalParameters.Count() == 3)
                {
                    //functionalAttributes_lb.IsEnabled = true;
                    functionalSpecComplete = true;
                    selected_env_att = selected_env_att + " ) ";
                    functionalParameters = new List<string>();
                    //activeInactiveCompsLBSelection();
                }
                else
                {
                    selected_env_att = selected_env_att + " , ";
                }

                if_then_rc_tb.AppendText(selected_env_att + " ");
                if_then_rc_formula.Add("attribute_" + comp_id, selected_env_att);
                comp_id = comp_id + 1;
               
            }

            environmentAttributes_lb.SelectedIndex = -1;
        }
                
        //public void activeInactiveCompsLBSelection()
        //{
        //    if (functionalSpecComplete == false || groupfunctionalSpecComplete == false)
        //    {
        //        return;
        //    }

        //    agentAttributes_lb.IsEnabled = false;
        //    environmentAttributes_lb.IsEnabled = false;
            
        //    union.IsEnabled = true;
        //    intersection.IsEnabled = true;

        //    or.IsEnabled = true;
        //    and.IsEnabled = true;
        //}
        

        //public void activeInactiveCompsOk_b_Selection()
        //{
        //    if (functionalSpecComplete == false || groupfunctionalSpecComplete == false)
        //    {
        //        return;
        //    }

        //    agentAttributes_lb.IsEnabled = false;
        //    environmentAttributes_lb.IsEnabled = false;
            
        //    union.IsEnabled = true;
        //    intersection.IsEnabled = true;
            
        //    or.IsEnabled = false;
        //    and.IsEnabled = false;
           
        //}

        private void Select_AggregationClose_Button_Click(object sender, RoutedEventArgs e)
        {
            if (source_attribute_ != null)
            {
                MisUseAnObjectsDB.loadAggregateAttributes(source_attribute_, MainWindow.clusterName);

                if (aiftaw_ != null)
                {
                    //aiftaw_.reloadAggregationList(source_attribute_);
                }
                else if (aaaw_ != null)
                {
                    aaaw_.reloadAggregationList(source_attribute_);
                }

            }
            this.Close();

        }
       
    }
}

